-
-
Notifications
You must be signed in to change notification settings - Fork 2k
stdlib: Add a _FilterProtocol type to logging's Filterer.addFilter() filter argument. #11018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment has been minimized.
This comment has been minimized.
Akuli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! A couple nits.
|
Also, I’m not sure about your merge habits (do you squash the PR or not) so I’m just going to keep amending to the original commit so there’s just one commit to merge. Keeps the history clean. |
77d7efd to
3525fcb
Compare
We always squash-merge at typeshed :) see our CONTRIBUTING guidelines: https://github.com/python/typeshed/blob/main/CONTRIBUTING.md#submitting-changes |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Akuli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is turning out trickier than expected :)
stdlib/logging/__init__.pyi
Outdated
| _ExcInfoType: TypeAlias = None | bool | _SysExcInfoType | BaseException | ||
| _ArgsType: TypeAlias = tuple[object, ...] | Mapping[str, object] | ||
| _FilterType: TypeAlias = Filter | Callable[[LogRecord], bool] | ||
| _FilterType: TypeAlias = _SupportsFilter | Callable[[LogRecord], bool] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Turns out we should keep
Filterhere, based on the mypy_primer output. Sphinx uses# type: ignorewhen they define a custom filter with a weirdfilter()method. IncludingFilterhere means that they don't get errors when adding the custom filter, only when defining it. Getting errors in only one place is better IMO. - On 3.12+, we should probably support the
LogRecordreturn type here too.
| _FilterType: TypeAlias = _SupportsFilter | Callable[[LogRecord], bool] | |
| if sys.version_info >= (3, 12): | |
| _FilterType: TypeAlias = Filter | _SupportsFilter | Callable[[LogRecord], bool | LogRecord] | |
| else: | |
| _FilterType: TypeAlias = Filter | _SupportsFilter | Callable[[LogRecord], bool] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commit eed6015 🤓
This comment has been minimized.
This comment has been minimized.
|
I was thinking that by making |
|
Pytest and whoever else needs to annotate "just some filter" can use |
Akuli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! One more nit.
stdlib/logging/__init__.pyi
Outdated
|
|
||
| if sys.version_info >= (3, 12): | ||
| class _SupportsFilter(Protocol): | ||
| def filter(self, record: LogRecord) -> bool | LogRecord: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make the argument positional-only in the protocol, so if your class names it something else than Record, it will still match the protocol:
| def filter(self, record: LogRecord) -> bool | LogRecord: ... | |
| def filter(self, __record: LogRecord) -> bool | LogRecord: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the change in commit fe68d1a, but I’m still a little unclear: is the dunder __record a convention that the name is now “flexible”?
It doesn’t make the record arg (not capitalized Record) positional-only, though, that would require a / as well, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type-checkers special-case __dunder-named arguments and treat them as positional-only. This is a bit surprising, and we will soon be able to switch to the new / syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is documented in PEP 484: https://peps.python.org/pep-0484/#positional-only-arguments
(PEP 484 predates PEP 570, which was only implemented in Python 3.8 -- typeshed still supports Python 3.7.)
|
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
Akuli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Closes #5700
As discussed in the related issue, starting with comment #5700 (comment).